package clientapp;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

public class addCustomer extends JFrame implements ActionListener
{
	private String lblNames[] = {"First Name:", "Surname:", "Phone Number:","Initial Credit:", "More Credit:"};
	private JLabel labels[];
	private JTextField textField[];
	
	private JLabel lblMain = new JLabel("Enter details:");
	
	private JButton btnAdd = new JButton("Add");
	private JButton btnCancel = new JButton("Cancel");  
	
	private JPanel cPanel = new JPanel();
	private JPanel sPanel = new JPanel(); 
	private JPanel cenPanel = new JPanel();
	private JPanel northPanel = new JPanel();
	private JPanel southPanel = new JPanel();
		
	private String custFirstName;
	private String surname;
	private String telNumber;
	
	private double initialCredit = 100.00;
	private double moreCredit = 0.0;
	private double totalCredit = 0.0;
	private boolean Exist = false;

    private Client clientr;
	public addCustomer(Client clientp)
	{
		super("Add new Customer");
                clientr = (Client)clientp;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(250,250);
		setResizable(false);
		setLocationRelativeTo(null);
		
		addWindowListener (new WindowAdapter ()
		{
            @Override
	    	public void windowClosing (WindowEvent e)
	    	{ //overiding windowClosing
				runStore.store.setEnabled(true);
	    	}
		} //the improved WindowListener is added.
		);
		
	    labels = new JLabel[lblNames.length];
	    textField = new JTextField[lblNames.length];
	    
	    GridLayout centerLayout = new GridLayout(6,2,5,5);
	    GridLayout btnLayout = new GridLayout(1,2,5,5);
	    GridLayout northLayout = new GridLayout(2,1,5,5);
	    
	    northPanel.setLayout(northLayout);
	    cPanel.setLayout(centerLayout);
	    sPanel.setLayout(btnLayout);
	    
	    cPanel.add(lblMain);
	    cPanel.add(new JPanel());
		
	    for(int A=0;A<lblNames.length;A++)
	    {
	   	 	labels[A] = new JLabel(lblNames[A]);
	   	 	cPanel.add(labels[A]);
	   		textField[A] = new JTextField();
	   		cPanel.add(textField[A]);	   		
	    }
		textField[3].setText("100.00");
		textField[3].setEditable(false);
			
		//northPanel.add(lblMain);
		
		sPanel.add(btnAdd);
		sPanel.add(btnCancel);
				
		southPanel.add(sPanel);
		cenPanel.add(cPanel);
		
		add(northPanel, BorderLayout.NORTH);
		add(cenPanel, BorderLayout.CENTER);
		add(southPanel, BorderLayout.SOUTH);
		
		btnCancel.addActionListener(this);
		btnAdd.addActionListener(this);
		
		setVisible(true);		
	}

    @Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == btnCancel)
		{
			int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to\n exit the customer form?", "Exit Customer", JOptionPane.YES_NO_OPTION);
			if(reply == JOptionPane.YES_OPTION)
			{
				dispose();
			}
		}
			
		else if (e.getSource() == btnAdd)
		{
			
				Exist = false;
				boolean fnIsGood = false;
				boolean snIsGood = false;
				boolean tnIsGood = true;
				boolean cIsGood = true;			
				custFirstName = textField[0].getText();
				if (!(custFirstName.equals("")))
				{
					fnIsGood = true;
				}
				surname = textField[1].getText();
				if (!(surname.equals("")))
				{
					snIsGood = true;
				}
				telNumber = textField[2].getText();
				if (!(telNumber.equals("")))
				{
					try 
					{
						Long.parseLong(textField[2].getText());
					}
					catch (Exception error)
					{
						tnIsGood = false;
					}
				}
				else
				{
					tnIsGood = false;
				}
				if (textField[4].getText().equals(""))
					textField[4].setText("0");
				try
				{
					moreCredit = Double.parseDouble(textField[4].getText());
				}
				catch (Exception error)
				{
					textField[4].requestFocus();
					cIsGood = false;
				}
				
				if(Exist)
				{
					JOptionPane.showMessageDialog(null, "Customer with the same number exist");
					textField[2].requestFocus();
				}
				else
				{
					if (fnIsGood && snIsGood && tnIsGood && cIsGood)
					{
						totalCredit = moreCredit + initialCredit;
						
						String name = textField[0].getText();
						String surname = textField[1].getText();
						String phone = textField[2].getText();
						double credits = totalCredit;
						
						String sendToServer = "AddCustomer" + "#" + name+ "#" + surname + "#" + phone + "#" + credits;
						
						clientr.sendQuery(sendToServer);
								
						JOptionPane.showMessageDialog(null, "Customer was successfully added" ,"Added info", JOptionPane.OK_OPTION);
						int responce = JOptionPane.showConfirmDialog(null, "Do you want to add more customers",
						"Add More", JOptionPane.YES_NO_OPTION);
							
						if(responce == JOptionPane.YES_OPTION)
						{
							textField[0].setText("");
							textField[1].setText("");
							textField[2].setText("");
							textField[4].setText("");
							textField[0].requestFocus();
						}
						else
						{
							dispose();
						}
					}
					else
					{
						String errorMsg = "";
							errorMsg += "Please correct the following\n";
						if (!fnIsGood)
							errorMsg += "\nFirst Name\n";
						if (!snIsGood)
							errorMsg += "Surname\n";
						if (!tnIsGood)
							errorMsg += "The Telephone number must be numeric\n";
						if (!cIsGood)
							errorMsg += "The Credit value must be numeric";
						JOptionPane.showMessageDialog(null, errorMsg);
					}		
				}
			}
		
	}
}